<?php
namespace app\common\model;
// 订单 处理业务

class OrderDetail extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_orderdetail';
    // 主键ID
    protected $pk = 'detail_id';

	/**
	 * @param array $where  // 查询的条件
	 * @param string $item  // 查询的参数
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();
        return $rs;
    }

//    订单详情插入操作
    function insertorderdetail($insertdata){
        if(!isset($insertdata['add_time'])){
            $insertdata['add_time'] = time();
        }
//        var_dump($insertdata);die;
        $data = [
            'user_id' => $insertdata['user_id'],
            'order_id' => $insertdata['order_id'],
            'product_id' => $insertdata['product_id'],
            'product_name' => $insertdata['product_name'],
            'p_s_id' => $insertdata['p_s_id'],
            'p_s_name' => $insertdata['p_s_name'],
            'cover_url' => $insertdata['cover_url'],
            'quantity' => $insertdata['quantity'],
            'price' => $insertdata['price'],
            'sale_price' => $insertdata['sale_price'],
            'growth_coin' => $insertdata['growth_coin'],
            'amount' => $insertdata['amount'],
            'add_time' => $insertdata['add_time'],
            'update_time' => $insertdata['add_time'],
        ];
        $rs = $this->save($data);
        if ($rs === false) {
            return false;
        }
        $orderid = $this->order_id;
        return $orderid;
    }


    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['detail_id' => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->detail_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

    // 列表
    function get_list($cond,$orderby,$item)
    {
        $where = null;

        if(!empty($cond['order_id'])){
            $where[] = ['order_id', '=',$cond['order_id']];
        }
        if(!empty($cond['user_id'])){
            $where[] = ['user_id', '=',$cond['user_id']];
        }

        $rs = $this
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->select()
            ->toArray();

        return $rs;
    }


}